import Vue from "vue";
import VueRouter from "vue-router";

import Home from "../views/Home.vue";
import Classify from "../views/Classify.vue";
import Cart from "../views/Cart.vue";
import Mine from "../views/Mine.vue";
import Login from "../views/Login.vue";
import Loginy from "../views/Loginy.vue";
import Reg from "../views/Reg.vue";

import Search from '../views/Search.vue';
import Goodsdetails from '../views/Goodsdetails.vue';
import Order from '../views/Order.vue'
import AddressEdit from '../views/AddressEdit.vue'
// import { component } from "vue/types/umd";

Vue.use(VueRouter);

const router = new VueRouter({
    routes: [{
            path: "/home",
            component: Home,
            name: "Home",
            props: true
        },
        //重定向路由
        {
            path: "/",
            redirect: "/home"
        },
        {
            path: "/classify",
            component: Classify,
            name: "Classify"
        },
        {
            path: "/cart",
            component: Cart,
            name: "Cart",
            meta: {
                requiresAuth: true
            }
        },
        {
            path: "/mine",
            component: Mine,
            name: "Mine",
            meta: {
                requiresAuth: true
            },
        },
        {
            path: '/search',
            component: Search,
            name: 'Search'
        },
        {
            path: '/goodsdetails',
            component: Goodsdetails,
            name: 'Goodsdetails'
        },
        {
            path: '/order',
            component: Order,
            name: 'Order'
        }, {
            path: '/addressedit',
            component: AddressEdit,
            name: 'AddressEdit'
        }, {
            path: '/addressedit',
            component: AddressEdit,
            name: 'AddressEdit'
        },
        {
            path: '/login',
            component: Login,
            name: 'Login'
        },
        {
            path: '/loginy',
            component: Loginy,
            name: 'Loginy'
        },
        {
            path: '/reg',
            component: Reg,
            name: 'Reg'
        }


    ],
});

router.beforeEach(function (to, from, next) {
    // 任何路由切换都会执行这里的代码
    console.log('beforeEach', to)
    // if(['/cart','/mine'].includes(to.path)){
    if (to.meta.requiresAuth) {
        const userInfo = JSON.parse(localStorage.getItem('userInfo')) || {}
        // 判断用户是否登录
        if (userInfo.authorization) {
            // 只要有token，不管是否有效，先放行
            next()

            // 放行后，校验token有效性
            // request.get('/user/verify',{
            // router.app.$request.get('/user/verify', {
            //     params: {
            //         username: userInfo.username
            //     },
            //     headers: {
            //         Authorization: userInfo.authorization
            //     }
            // }).then(({
            //     data
            // }) => {
            //     if (data.code === 400) {
            //         localStorage.removeItem('userInfo')
            //         router.push({
            //             path: '/login',
            //             query: {
            //                 targetUrl: to.fullPath
            //             }
            //         })
            //     }
            // })
        } else {
            router.push({
                path: '/login',
                query: {
                    targetUrl: to.fullPath
                }
            })
        }
    } else {

        next();
    }
})
export default router